<?php
ini_set('error_reporting', e_notice);
include_once "inc_function.php";
session_start();
?>
<link rel="stylesheet" type="text/css" href="<?= 'http://'.CSS_PATH;?>layout_reg_screen.css">
<script type="text/javascript" src="<?= 'http://'.JS_PATH;?>jquery-1.6.2.min.js"></script>
<script>
  function onSubmit(){
      var person = $("#person").val();
      var country = $('#c_name').val();
      if( country == ''){
         alert('please fill out the data!');
         return false;
      }else if(person == ''){
         alert('please Login First!');
         return false;
      }else{
       	return true;

      }
   }
</script>

<body>
<?php

//  $hidden = ($_REQUEST['form'] == "add" && isset($_REQUEST['form']))? "display:block": "display:none";
  $text = ($_REQUEST['form'] == "update")? "Update": "Add";

  if($_REQUEST['form'] != "update"){
    $record = oci_parse($con,"select max(country_id)+1 as last_id from m_country");
    oci_execute($record,OCI_DEFAULT);
    $last_id = oci_fetch_array($record);
  }

  switch($_POST['action']){
      case "Add":
         $record = oci_parse($con,"begin pkg_master.sp_insert_m_country(:country_name,:update_id,:create_id);end;");
         oci_bind_by_name($record, ':country_name',$_POST['c_name']);
         oci_bind_by_name($record, ':update_id',$_POST['person']);
         oci_bind_by_name($record, ':create_id',$_POST['person']);
         oci_execute($record,OCI_DEFAULT);

         	$r = oci_commit($con);
			// Commit the changes to both tables
			if (!$r) 
			{	
				echo "<script>alert(connection failure, please try again!!!)</script>";				
		    	$e = oci_error($con);
				oci_rollback($con);
			}								                  
         
           header("Location: list_country.php");
      break;
      case "Update":
         $record = oci_parse($con,"begin pkg_master.sp_update_m_country(:country_id,:country_name,:update_id);end;");
         oci_bind_by_name($record, ':country_name',$_POST['c_name']);
         oci_bind_by_name($record, ':update_id',$_POST['person']);
         oci_bind_by_name($record, ':country_id',$_POST['c_code']);
         oci_execute($record,OCI_DEFAULT);
//         var_dump($_POST);
         	$r = oci_commit($con);
			// Commit the changes to both tables
			if (!$r) 
			{	
				echo "<script>alert(connection failure, please try again!!!)</script>";				
		    	$e = oci_error($con);
				oci_rollback($con);
			}								
         
           header("Location: list_country.php");
      break;
      default :
      break;
   }

   if($_REQUEST['form'] == "update" && ($_REQUEST['update'] != 0 && isset($_REQUEST['update']))){
      $curs = oci_new_cursor($con);
   	$stmt = OCIParse($con,"begin pkg_master.sp_get_country_by_id(:country_id,:o_country_list); end;");
   	OCIBindByName($stmt,":country_id",$_REQUEST['update']);
   	OCIBindByName($stmt,":o_country_list",&$curs,-1,OCI_B_CURSOR);
   	ociexecute($stmt,OCI_DEFAULT);
   	ociexecute($curs,OCI_DEFAULT);

   	while (OCIFetchInto($curs,&$o_country_list))
  	   {
       	$id = $o_country_list[0];
       	$country = $o_country_list[1];
       	$person = $o_country_list[2];
       	$date_create = $o_country_list[3];
       	$date_modify = $o_country_list[5];
  	   }
   }
   ?>
<form class="form" action method="post" onsubmit="return onSubmit();">
<input type="hidden" name="action" value="<?=$text;?>" />
<input type="hidden" name="person" id="person" value="<?= $_SESSION['employee_id'];?>" />
<input type="hidden" name="c_code" value="<?= $id;?>" />

<fieldset class="f_country">
			<legend><?= $text;?> Country</legend>
         <div class="formInput">
				<label for="country_code">Country Code :</label> <input type="text" id="c_code" class="textbox" name="tmp_code" disabled value="<?= ($_REQUEST['form'] != "update")?$last_id['LAST_ID']:$id;?>"> <img class="hidden" src="http://<?= IMAGE_PATH?>search_engine1.png" class="luv_search" style="<?=$hidden?>" width="25px" onclick="window.open('http://<?= ABSPATH?>lookup/lk_pic.php','myExample6','width=400,height=400,top=400,left=500,scrollbars=yes')"/>
			</div>
         <div class="formInput">
				<label for="country_name">Country Name :</label> <input type="text" id="c_name" class="textbox" name="c_name" value="<?= $country?>" maxlength=20>
			</div>
			<div class="mybutton"> <button type="submit" class="button"> Save </button> <button type="reset" class="button"> Reset </button></div>
			<div align="right"><a href='list_country.php'>Back to <span>List Country</span></a></div>

</fieldset>
</form>
   </body>